package com.chancingpack.controller;

import com.alibaba.fastjson.JSONObject;
import com.chancingpack.bean.WxUserLoginBean;
import com.chancingpack.dal.model.WxUserLogin;
import com.chancingpack.service.WxUserLoginService;
import com.chancingpack.util.CommonPage;
import com.chancingpack.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/17.
 */
@Controller
@RequestMapping("/WxUserLoginController")
public class WxUserLoginController {
    @Autowired
    private WxUserLoginService wxUserLoginService;

    @RequestMapping("/getList")
    public ModelAndView getList(){
        ModelAndView view=new ModelAndView("/wxUserLogin/wxUserLoginList");
        List<WxUserLogin> WxUserLoginList=wxUserLoginService.getList();
        view.addObject("WxUserLoginList", WxUserLoginList);
        return view;
    }

    /**
     * 分页显示用户信息
     * @param commonPage
     * @return CommonResult
     */
    @RequestMapping(value="/getLists",produces="application/json;charset=UTF-8")
    @ResponseBody
    public CommonResult getList(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response){
        CommonResult result = this.wxUserLoginService.getListByPage(commonPage,request,response);
        if (result.getIsSuccess()) {
            result.setData(commonPage);
        }
        return result;
    }

    //to添加用户信息
    @RequestMapping("/toInsertPage")
    public ModelAndView toInsertPage(){
        ModelAndView view=new ModelAndView("/wxUserLogin/wxUserLoginAdd");
        //List<Map<String,Object>> wxUserLoginListMap=wxUserLoginService.getListMap(null);
        List<Map<String,Object>> sgbasCustInfoMap=wxUserLoginService.getSgbasCustInfo();
        List<Map<String,Object>> roleMap=wxUserLoginService.getRole();
        //view.addObject("wxUserLoginListMap", wxUserLoginListMap);
        view.addObject("sgbasCustInfoMap", sgbasCustInfoMap);
        view.addObject("roleMap", roleMap);
        return view;
    }

    /**
     * 增加用户信息
     * @param
     * @return JSON
     */
    @RequestMapping(value="/insertRecord",produces="application/json;charset=UTF-8")
    @ResponseBody
    public String insertRecord(@RequestBody WxUserLogin record){
        CommonResult result = this.wxUserLoginService.insertRecord(record);
        return JSONObject.toJSONString(result);
    }

    @RequestMapping("/toEdit")
    public ModelAndView toEdit(@RequestParam(value = "userloginId") Integer userloginId){
        ModelAndView view=new ModelAndView("/wxUserLogin/wxUserLoginEdit");
        List<Map<String,Object>> wxUserLoginListMap=wxUserLoginService.getListMap(userloginId);
        Map<String,Object> wxUserLoginMap=wxUserLoginListMap.get(0);
        List<Map<String,Object>> sgbasCustInfoMap=wxUserLoginService.getSgbasCustInfo();
        List<Map<String,Object>> roleMap=wxUserLoginService.getRole();
        view.addObject("wxUserLoginMap", wxUserLoginMap);
        view.addObject("sgbasCustInfoMap", sgbasCustInfoMap);
        view.addObject("roleMap", roleMap);
        List<WxUserLogin> WxUserLoginList=wxUserLoginService.getList();
        view.addObject("WxUserLoginList", WxUserLoginList);
        return view;
    }

    /**
     * 修改用户信息
     * @param
     * @return JSON
     */
    @RequestMapping(value="/updateRecord",produces="application/json;charset=UTF-8")
    @ResponseBody
    public String updateRecord(@RequestBody WxUserLogin record){
        CommonResult result = this.wxUserLoginService.updateRecord(record);
        return JSONObject.toJSONString(result);
    }
    /**
     * 删除用户信息
     * @param
     * @return JSON
     */
    @RequestMapping(value="/deleteRecord")
    @ResponseBody
    public String deleteRecord(@RequestParam(value = "userloginId") Integer userloginId){
        CommonResult result =this.wxUserLoginService.deleteRecord(userloginId);
        //CommonResult result = this.roleBiz.deleteRecord(roleId);
        return JSONObject.toJSONString(result);
    }
}
